<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 密码类别
 */
class CategoryController extends HomeController {
	private $category;
	/**
	 * 构造函数
	 */
	public function _initialize() {
		parent::_initialize();
		$this->category = M('Category');
	}
	/**
	 * 首页
	 */
	public function index() {
		$map = array();
		$map['uid'] = $this->uid;
		
		$list = $this->category->where($map)->field('id,parent_id,category_name,password_num')->order('order_num ASC')->select();
		$tree = new \Org\Util\Tree($list);
		// 最外层必须是双引号，变量需要替换为$id的形式
		$leaf = "<span><i class='icon icon-folder-open'></i>\$category_name(\$password_num)</span>&nbsp;&nbsp;";
		$leaf .= "<a href='". U('Category/add', array('id'=>'_id_')) ."' class='btn btn-mini'>添加子类</a> | ";
		$leaf .= "<a href='". U('Category/edit', array('id'=>'_id_')) ."' class='btn btn-mini'>修改</a> | ";
		$leaf .= "<a href='". U('Category/delete', array('id'=>'_id_')) ."' class='btn btn-mini'>删除</a>";
		$leaf = str_replace('_id_', '$id', $leaf);
		
		$branch = "<span><i class='icon icon-folder-close'></i>\$category_name(\$password_num)</span>&nbsp;&nbsp;";
		$branch .= "<a href='". U('Category/add', array('id'=>'_id_')) ."' class='btn btn-mini'>添加子类</a> | ";
		$branch .= "<a href='". U('Category/edit', array('id'=>'_id_')) ."' class='btn btn-mini'>修改</a> | ";
		$branch .= "<a href='". U('Category/delete', array('id'=>'_id_')) ."' class='btn btn-mini'>删除</a>";
		$branch = str_replace('_id_', '$id', $branch);
		
		$treeview = $tree->get_treeview(0, 'pwd_tree" data-ride="tree', $leaf, $branch, 0, 'tree-lines');
		
		$this->assign('treeview', $treeview);
		$this->display();
	}
	/**
	 * 添加
	 */
	public function add() {
		if (IS_POST) {
			$parent_id = I('post.parent_id');
			$order_num = I('post.order_num');
			$category_name = I('post.category_name');
			
			$data = array();
			$data['parent_id'] = $parent_id;
			$data['uid'] = $this->uid;
			$data['order_num'] = $order_num;
			$data['category_name'] = $category_name;
			$data['create_time'] = get_time();
			$data['update_time'] = '0000-00-00 00:00:00';
			$data['password_num'] = 0;
			
			if (false !== $this->category->add($data)) {
				$this->success('类别添加成功');
			}else {
				$this->error('类别添加失败');
			}
		}else {
			$parent_id = I('get.id');
			$map = array();
			$map['uid'] = $this->uid;
			
			$list = $this->category->where($map)->field('id,parent_id,category_name,password_num')->order('order_num ASC')->select();
			$tree = new \Org\Util\Tree($list);
			$str = "<option value='\$id' \$selected>\$spacer\$category_name</option>";
			$parent_id_options = $tree->get_tree(0, $str, $parent_id);
			
			$this->assign('parent_id_options', $parent_id_options);
			$this->display();
		}
	}
	/**
	 * 编辑
	 */
	public function edit() {
		if (IS_POST) {
			$id = I('post.id');
			$parent_id = I('post.parent_id');
			$order_num = I('post.order_num');
			$category_name = I('post.category_name');
			
			$map = array();
			$map['id'] = $id;
			$map['uid'] = $this->uid;
			
			$data = array();
			$data['parent_id'] = $parent_id;
			$data['order_num'] = $order_num;
			$data['category_name'] = $category_name;
			$data['update_time'] = get_time();
			
			if (false !== $this->category->where($map)->save($data)) {
				$this->success('类别修改成功');
			}else {
				$this->error('类别修改失败');
			}
		}else {
			$id = I('get.id');
			$where = array();
			$where['id'] = $id;
			$where['uid'] = $this->uid;
			
			$data = $this->category->where($where)->field(true)->find();
			if (is_null($data)) {
				$this->error('没有这个类别');
			}
			
			$map = array();
			$map['uid'] = $this->uid;
			
			$list = $this->category->where($map)->field('id,parent_id,category_name,password_num')->order('order_num ASC')->select();
			$tree = new \Org\Util\Tree($list);
			$str = "<option value='\$id' \$selected>\$spacer\$category_name</option>";
			$parent_id_options = $tree->get_tree(0, $str, $data['parent_id']);
			
			$this->assign('data', $data);
			$this->assign('parent_id_options', $parent_id_options);
			$this->display();
		}
	}
	/**
	 * 删除
	 */
	public function delete() {
		$id = I('get.id');
		// 是否有子类
		$where = array();
		$where['parent_id'] = $id;
		$where['uid'] = $this->uid;
		$count = $this->category->where($where)->count();
		if (0 < $count) {
			$this->error('你不能删除它，因为存在子类');
		}
		
		$map = array();
		$map['id'] = $id;
		$map['uid'] = $this->uid;
		
		$password_num = $this->category->where($map)->getField('password_num');
		if (0 < $password_num) {
			$this->error('你不能删除它，因为这个类别下面存在密码');
		}
		$result = $this->category->where($map)->delete();
		if (false !== $result && 0 < $result) {
			$this->error('类别删除成功');
		}else {
			$this->error('类别删除失败');
		}
	}
}